package com.wcgksoft.controller.display;


import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.display.Monitoringdata;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.display.MonitoringdataService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "monitoringdat")
public class MonitoringdataController {


    @Autowired
    private MonitoringdataService monitoringdataService;


    @Autowired
    private EnterpriseProjectService enterpriseProjectService;




    @RequestMapping("/ret")
    public String ret(ModelMap modelMap){
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseProject.class);
        criteria.add(Restrictions.eq("id", sysUser.getAreaid()));
        List<EnterpriseProject> byConditions = enterpriseProjectService.findByConditions(criteria);

       modelMap.put("arealist",byConditions );


        return "display/list";
    }


    /**
     * 数据展示列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public Layui findAll(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        DetachedCriteria criteria = DetachedCriteria.forClass(Monitoringdata.class);
        criteria.add(Restrictions.eq("areaid", sysUser.getAreaid()));
        List<Monitoringdata> templateList = monitoringdataService.findByConditions(criteria, page, limit);
        return Layui.data(0, "查询成功", monitoringdataService.getTotalCount(), templateList);

    }



    /*
     * 数据展示添加/修改
     *
     * */
    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(@RequestParam(value = "data", required = false) String data, ModelMap modelMap) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            Monitoringdata monitoringdata = (Monitoringdata) JSONObject.toBean(JSONObject.fromObject(data), Monitoringdata.class);
            monitoringdata.setAreaid(sysUser.getAreaid());
            monitoringdataService.saveOrUpdate(monitoringdata);
            return Layui.data(0, "修改成功", 1, Layui.addList(monitoringdata));
        }
    }


    /**
     * 删除
     * @param id
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value="delete")
    public Layui delete(@RequestParam(value = "id") Integer id){
        Monitoringdata monitoringdata=monitoringdataService.findById(id);
        monitoringdataService.delete(monitoringdata);
        return Layui.data(1,"删除成功",1,null);
    }









}
